import com.cherry.mybatis.Emp;
import com.cherry.mybatis.dao.EmpAnnoDao;
import com.cherry.mybatis.dao.EmpDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class T04 {

    private EmpAnnoDao empDao;
    private SqlSession sqlSession;

    @Before
    public void before() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        sqlSession = sqlSessionFactory.openSession();
        empDao = sqlSession.getMapper(EmpAnnoDao.class);
    }


    @Test
    public void i01() {
        Emp test01 = new Emp("哈哈哈", "男", 200.0, new Date(), 2);
        empDao.add(test01);
    }

    @Test
    public void u01() {
        Emp test01 = new Emp();
        test01.setId(9);
        test01.setName("嘻嘻");
        empDao.update(test01);
    }

    @Test
    public void d01() {
        empDao.delete(9L);
    }

    @Test
    public void q01() {
        System.out.println(empDao.findAll());
    }


    @After
    public void after() {
        //注意点：增、删、改操作需要提交事务！
        sqlSession.commit();
        sqlSession.close();
    }

}
